package com.htwy.kits;

import java.io.File;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.SQLExec;
import org.apache.tools.ant.types.EnumeratedAttribute;

public class SqlFileKit {

    public static void executeSqlFile(File sqlFile, String dbDriver, String jdbcUrl, String dbAccount, String dbPwd) {
        SQLExec sqlExec = new SQLExec();
        sqlExec.setDriver(dbDriver);
        sqlExec.setUrl(jdbcUrl);
        sqlExec.setUserid(dbAccount);
        sqlExec.setPassword(dbPwd);

        sqlExec.setSrc(sqlFile);

        sqlExec.setOnerror((SQLExec.OnError) (EnumeratedAttribute.getInstance(SQLExec.OnError.class, "abort")));
        sqlExec.setPrint(true); //设置是否输出
//        输出到文件 sql.out 中；不设置该属性，默认输出到控制台
//        sqlExec.setOutput(new File("src/sql.out"));
        sqlExec.setProject(new Project()); // 要指定这个属性，不然会出错
        sqlExec.execute();
    }

}
